package com.livestocksolutions.ui;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;

import com.livestocksolutions.Bovine;
import com.livestocksolutions.LivestockSolutions;
import com.livestocksolutions.util.Comparators;
import com.livestocksolutions.util.JDatePickerDate;
import com.livestocksolutions.util.Table;
import javax.swing.SwingConstants;
import javax.swing.JButton;

/**
 * 
 * @author Matt Lang
 *
 */
public class ProductiveParentPanel extends JPanel
{
	/**
	 * Default value for serializing the data
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor.
	 */
	public ProductiveParentPanel()
	{
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("Offspring Weight Gain");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
		panel.add(lblNewLabel);
		
		JPanel panel_4 = new JPanel();
		add(panel_4, BorderLayout.CENTER);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_4.add(panel_1, BorderLayout.NORTH);
		
		JButton btnMostProductive = new JButton("Show Most Weight Gain");
		panel_1.add(btnMostProductive);
		
		JButton btnLeastProductive = new JButton("Show Least Weight Gain");
		panel_1.add(btnLeastProductive);
		
		final JPanel displayPanel = new JPanel();
		panel_4.add(displayPanel, BorderLayout.CENTER);
		
		btnLeastProductive.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				displayPanel.removeAll();
				displayPanel.add(getLeastProductive());
				displayPanel.validate();
			}
		});
		
		btnMostProductive.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				displayPanel.removeAll();
				displayPanel.add(getMostProductive());
				displayPanel.validate();
			}
		});
		
	}
	
	/**
	 * Calculate a list of least productive cattle based off the value of their offspring.
	 * @return A Table containing a list of the least productive cattle.
	 */
	public Table getLeastProductive()
	{
		Table returnPanel = new Table("Least Productive Parents", true, "Visual Tag", "Status", "DOB", "Number of Offspring", "Calf ADG");
		returnPanel.setEmptyMessage("There is insufficient information to show least productive at this time");
		
		Bovine[] bovines = LivestockSolutions.getBovines();
		ArrayList<Bovine> sortedList = new ArrayList<Bovine>(Arrays.asList(bovines));
		Iterator<Bovine> iterator = sortedList.iterator();
		while(iterator.hasNext())
		{
			Bovine b = iterator.next();
			if (b.getOffspringAverageDailyGrowth()==Double.NEGATIVE_INFINITY)
			{
				iterator.remove();
			}
		}
		Collections.sort(sortedList, Comparators.bovineOffspringAverageWeightGain);
		int maxCap=30;
		if(sortedList.size() <= maxCap)
			maxCap = sortedList.size();
		
		if(sortedList.size() > 0) {
			for(int i=0;i<maxCap;i++)
			{
				returnPanel.addRow(new JLabel(sortedList.get(i).getEartag().getEartagID()),
						new JLabel(sortedList.get(i).getStringStatus()),
						new JLabel(JDatePickerDate.format(sortedList.get(i).getDob())),
						new JLabel(String.valueOf(sortedList.get(i).getOffspring().length)),
						new JLabel(String.valueOf(new DecimalFormat("#.###").format(sortedList.get(i).getOffspringAverageDailyGrowth()))));
				final long id = sortedList.get(i).getID();
				returnPanel.addMouseListener(new MouseListener() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if(MainFrame.getInstance().showBovine(id)) {
							//show cattle not found message?
						}
					}
					@Override public void mouseEntered(MouseEvent arg0) {}
					@Override public void mouseExited(MouseEvent arg0) {}
					@Override public void mousePressed(MouseEvent arg0) {}
					@Override public void mouseReleased(MouseEvent arg0) {}
				});
			}
		}
		
		return returnPanel;
		
	}
	
	/**
	 * Calculate a list of most productive cattle based off the value of their offspring.
	 * @return A Table containing a list of the most productive cattle.
	 */
	public Table getMostProductive()
	{
		Table returnPanel = new Table("Top 30 Most Productive Parents", true, "Visual tag", "Status", "DOB", "Number of Offspring", "Calf ADG");
		returnPanel.setEmptyMessage("There is insufficient information to show most productive at this time");
		Bovine[] bovines = LivestockSolutions.getBovines();
		ArrayList<Bovine> sortedList = new ArrayList<Bovine>(Arrays.asList(bovines));
		Iterator<Bovine> iterator = sortedList.iterator();
		while(iterator.hasNext())
		{
			Bovine b = iterator.next();
			if (b.getOffspringAverageDailyGrowth()==Double.NEGATIVE_INFINITY)
			{
				iterator.remove();
			}
		}
		Collections.sort(sortedList, Collections.reverseOrder(Comparators.bovineOffspringAverageWeightGain));
		int maxCap=30;
		if(sortedList.size()>0) {
			if(sortedList.size() <= maxCap)
				maxCap = sortedList.size();
			for(int i=0;i<maxCap;i++)
			{
				returnPanel.addRow(new JLabel(sortedList.get(i).getEartag().getEartagID()),
					new JLabel(sortedList.get(i).getStringStatus()),
					new JLabel(JDatePickerDate.format(sortedList.get(i).getDob())),
					new JLabel(String.valueOf(sortedList.get(i).getOffspring().length)),
					new JLabel(String.valueOf(new DecimalFormat("#.###").format(sortedList.get(i).getOffspringAverageDailyGrowth()))));
				
				final long id = sortedList.get(i).getID();
				returnPanel.addMouseListener(new MouseListener() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						if(MainFrame.getInstance().showBovine(id)) {
							//show cattle not found message?
						}
					}
					@Override public void mouseEntered(MouseEvent arg0) {}
					@Override public void mouseExited(MouseEvent arg0) {}
					@Override public void mousePressed(MouseEvent arg0) {}
					@Override public void mouseReleased(MouseEvent arg0) {}
				});
				
			}
		}
		return returnPanel;
	}
}
